namespace com.stifel.ProjectMgmt
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Runtime.InteropServices;
    using System.Web;

    using com.stifel.Extensions.StringExtensions;
    using com.stifel.ProjectMgmtDao;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;

    [Guid( "73c4cacf-5823-4bfc-b125-4e11dae12575" )]
    public partial class ExportProjectsToExcel : IHttpHandler
    {
        #region Properties

        /// <summary>
        /// Gets a value indicating whether another request can use the <see cref="T:System.Web.IHttpHandler"/> instance.
        /// </summary>
        /// <value></value>
        /// <returns>true if the <see cref="T:System.Web.IHttpHandler"/> instance is reusable; otherwise, false.
        /// </returns>
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        #endregion Properties

        #region Methods

        /// <summary>
        /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"/> interface.
        /// </summary>
        /// <param name="context">An <see cref="T:System.Web.HttpContext"/> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param>
        public void ProcessRequest( HttpContext context )
        {
            string url = context.Request.QueryString.Get( "q" );
            process( context, url );
            return;
        }

        void process( HttpContext context, string url )
        {
            SPSecurity.RunWithElevatedPrivileges( delegate( )
            {
                using ( SPSite spSite = new SPSite( url ) )
                {
                    using ( SPWeb spWeb = spSite.OpenWeb( ) )
                    {
                        HttpRequest httpRequest = new HttpRequest( "", spWeb.Url, "" );
                        HttpContext.Current = new HttpContext( httpRequest, new HttpResponse( new StringWriter( ) ) );
                        SPControl.SetContextWeb( HttpContext.Current, spWeb );
                        StifelProjectDao dao = new StifelProjectDao( );
                        List<ProjectSite> sites = dao.GetSitesWithProjectsDefined( url );
                        string projects = dao.GetCurrentProjectsAsTsv( sites );
                        if ( string.IsNullOrEmpty( projects ) )
                            projects = "No projects found.";
                        context.Response.BufferOutput = true;
                        context.Response.ContentType = "text/tab-separated-values";
                        context.Response.AddHeader( "Content-Disposition", string.Format( "attachment;filename=stifelprojects_{0}.tsv", DateTime.Now.ToString( "yyyy.MM.dd_HH.mm.ss" ) ) );
                        byte[ ] s = projects.ToByteArray( );
                        context.Response.OutputStream.Write( s, 0, s.Length );
                        context.Response.OutputStream.Flush( );
                        context.Response.OutputStream.Close( );
                    }
                }
            } );
        }

        #endregion Methods
    }
}